//
//  OrderBuyIntroCell.m
//  danpin
//
//  Created by chuangjia on 27/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderBuyIntroCell.h"
#import "TTTAttributedLabel.h"
#import "CalcLabel.h"

@interface OrderBuyIntroCell ()<TTTAttributedLabelDelegate>
{
    
}
@property(nonatomic,strong)UIButton *helpbtn;
@property(nonatomic,strong) TTTAttributedLabel *lbMessage;  //日期


@end
@implementation OrderBuyIntroCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
    
        //告诉用户总价的计算方式，各个细项的收费原因
        _helpbtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _helpbtn.backgroundColor = [UIColor whiteColor];
        _helpbtn.layer.borderWidth=1;
        _helpbtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _helpbtn.tag=1;
        [_helpbtn setSelected:YES];
        _helpbtn.titleLabel.font = FONT(12);
        [_helpbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_helpbtn setTitle:@"√" forState:UIControlStateNormal];
        _helpbtn.layer.borderColor = [UIColor redColor].CGColor;
        _helpbtn.frame =CGRectMake(PHOTO_FRAME_WIDTH,(40-16)/2, 16, 16);
        [_helpbtn addTarget:self action:@selector(clickHelpEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_helpbtn];
        
        _lbMessage =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*3,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6,40)];
        _lbMessage.backgroundColor =  [UIColor clearColor];
        _lbMessage.textColor = CONTEXT_COLOR;//UIColorFromRGB(0x808080);
        _lbMessage.textAlignment = NSTextAlignmentLeft;
        _lbMessage.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _lbMessage.numberOfLines=0;
        _lbMessage.delegate =self;
        _lbMessage.font = FONT(12);
        _lbMessage.delegate =self;
        [self.contentView addSubview:_lbMessage];
        
        
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    
    [self.lbMessage removeFromSuperview];
    self.lbMessage=nil;
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    _helpbtn.frame =CGRectMake(PHOTO_FRAME_WIDTH,(40 -PHOTO_FRAME_WIDTH*1.5)/2, PHOTO_FRAME_WIDTH*1.5, PHOTO_FRAME_WIDTH*1.5);
    //
    float fheight = 0;
    NSString *title = @"我接受《服务协议》、《退换协议》和《隐私协议》。";
    NSString *express_no =@"《服务协议》";
    NSString *express_no1 =@"《退换协议》";
    NSString *express_no2 =@"《隐私协议》";
    
    if (title.length>0) {
        [self.lbMessage setText:title afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             [attributedString addAttribute:NSFontAttributeName
                                      value:FONT(12)
                                      range:NSMakeRange(0,[attributedString length])];

             CGFloat lineSpace = 2.0f;
             CTParagraphStyleSetting lineSpaceStyle;
             lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
             lineSpaceStyle.valueSize = sizeof(lineSpace);
             lineSpaceStyle.value =&lineSpace;
             
             //设置文本段间距
             CGFloat paragraphSpacing = 0.0f;
             CTParagraphStyleSetting paragraphSpaceStyle;
             paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
             paragraphSpaceStyle.valueSize = sizeof(CGFloat);
             paragraphSpaceStyle.value = &paragraphSpacing;
             
             //创建设置数组
             CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
             CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
             
             //给文本添加设置
             [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
             return attributedString;
             
         }];
        fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*4) str:self.lbMessage.attributedText];
        self.lbMessage.frame =CGRectMake(PHOTO_FRAME_WIDTH*3.5, PHOTO_FRAME_WIDTH+1, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*5, fheight);
        fheight =PHOTO_FRAME_WIDTH*2+fheight;
        //
        NSRange range = [title rangeOfString:express_no];
        NSRange range1 = [title rangeOfString:express_no1];
        NSRange range2 = [title rangeOfString:express_no2];
        
        if (range.location != NSNotFound) {
            //设置字体颜色
            [self.lbMessage addLinkToURL:[NSURL URLWithString:@"danpin1://"] withRange:range];
            
        }else{
           
        }
        if (range1.location != NSNotFound) {
            //设置字体颜色
            [self.lbMessage addLinkToURL:[NSURL URLWithString:@"danpin2://"] withRange:range1];
            
        }else{
            
        }
        if (range2.location != NSNotFound) {
            //设置字体颜色
            [self.lbMessage addLinkToURL:[NSURL URLWithString:@"danpin3://"] withRange:range2];
            
        }else{
            
        }
    }
    
    [self layoutSubviews];
}
- (void)attributedLabel:(__unused TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_agreement,nil,self.data,url.scheme,self.indexPath);
    }
    
}

-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)clickHelpEvent:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    if (btn.isSelected) {
        [btn setSelected:NO];
        [_helpbtn setTitle:@"" forState:UIControlStateNormal];
        _helpbtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }else{
        [btn setSelected:YES];
         [_helpbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_helpbtn setTitle:@"√" forState:UIControlStateNormal];
        _helpbtn.layer.borderColor = [UIColor redColor].CGColor;
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    CGFloat fheight = 0;
    NSString *title =@"我接受《服务协议》、《退换协议》和《隐私协议》。";
    if(title.length>0)
    {
        NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:title];
        
        [attributedString addAttribute:NSFontAttributeName
                                 value:FONT(12)
                                 range:NSMakeRange(0,[attributedString length])];

        [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:CONTEXT_COLOR range:NSMakeRange(0,[attributedString length])];
        
        CGFloat lineSpace = 2.0f;
        CTParagraphStyleSetting lineSpaceStyle;
        lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
        lineSpaceStyle.valueSize = sizeof(lineSpace);
        lineSpaceStyle.value =&lineSpace;
        
        //设置文本段间距
        CGFloat paragraphSpacing = 0.0f;
        CTParagraphStyleSetting paragraphSpaceStyle;
        paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
        paragraphSpaceStyle.valueSize = sizeof(CGFloat);
        paragraphSpaceStyle.value = &paragraphSpacing;
        
        //创建设置数组
        CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
        CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
        
        //给文本添加设置
        [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
        
        fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*4) str:attributedString];
        attributedString =nil;
    }
    return fheight+PHOTO_FRAME_WIDTH*2;
}
@end
